package cn.edu.sjtu.se.dslab.pojo;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

public class CodeGenerator {
	
	protected VelocityEngine ve = null;
	protected VelocityContext vc = null;
	protected Template template = null;
	
	public static final String TEMPLATE_PATH = "/velocity";
	private static String ABSOLUTE_ROOT_PATH;
	{
		ABSOLUTE_ROOT_PATH = CodeGenerator.class.getResource(TEMPLATE_PATH).getPath();
	}
	
	public CodeGenerator() {
		ve = new VelocityEngine();
		Properties properties = new Properties();
		properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, ABSOLUTE_ROOT_PATH);
		ve.init(properties);
		vc = new VelocityContext();
	}
	
	
	public CodeGenerator(String templateFile) {
		this();
		template = ve.getTemplate(templateFile);
	}

	public void loadTemplage(String templateFile) {
		template = Velocity.getTemplate(templateFile);
	}


	public VelocityEngine getVe() {
		return ve;
	}


	public void setVe(VelocityEngine ve) {
		this.ve = ve;
	}


	public VelocityContext getVc() {
		return vc;
	}


	public void setVc(VelocityContext vc) {
		this.vc = vc;
	}


	public Template getTemplate() {
		return template;
	}


	public void setTemplate(Template template) {
		this.template = template;
	}
	
	protected File getFile(String targetFileName) throws IOException{
		File targetFile = new File(targetFileName);
		System.out.println(targetFileName);
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}
		if (!targetFile.exists()){
			targetFile.createNewFile();
		}
		return targetFile;
	}
}
